package com.esteel.settlement.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.esteel.settlement.entity.TbSettAccount;
import com.esteel.settlement.entity.TbSettFundChg;
import com.esteel.settlement.entity.TbSettPay;
import com.esteel.settlement.pa.packet.Answer;
import com.esteel.settlement.pa.packet.PlatformOperationPaymentPB1331;
import com.esteel.settlement.service.SettAccountService;
import com.esteel.settlement.service.SettDictionaryService;
import com.esteel.settlement.service.SettFundChgService;
import com.esteel.settlement.service.SettFundService;
import com.esteel.settlement.service.SettPayService;
import com.esteel.settlement.vo.FunChgQueryVo;
import com.esteel.settlement.vo.FundQueryVo;
import com.esteel.settlement.vo.PayQueryVo;
import com.esteel.settlement.vo.TbSettFundVo;
import com.esteel.settlement.vo.TbSettPayVo;

/**
 * 会员账户操作接口
 * @author zhangxiuzhi
 *
 */
@Controller
public class AccountController {

	@Autowired
	private SettAccountService settAccountService;
	
	@Autowired
	private SettPayService settPayService;
	
	@Autowired
	private SettDictionaryService dict;
	
	@Autowired
	private SettFundService settFundService;
	
	@Autowired
	private SettFundChgService settFundChgService;

	
	private Logger logger =  LoggerFactory.getLogger(this.getClass());

	
	@RequestMapping("/account")
	public String index(Model model){
		return "account";
	}
	
	@RequestMapping("/payList")
	public String payList(){
		return "payList";
	}
	
	
	/**
	 * 查询所有的会员账号
	 * @param page
	 * @param sizePerPage
	 * @return
	 */
	@RequestMapping("accountList")
	@ResponseBody
	public  Page<TbSettFundVo> findAll(FundQueryVo fundQueryVo){
		 return settFundService.findAll(fundQueryVo);
	}
	
	@RequestMapping("/payInfoList")
	@ResponseBody
	public Page<TbSettPayVo> payList(PayQueryVo payQueryVo){
		
		 return settPayService.payQuery(payQueryVo);
	}

	/**
	 * 获取资金冻结与解冻的页面
	 * @param model
	 * @param custAcctId
	 * @return
	 */
	@RequestMapping("freeze")
	public String freezePage(Model model,String custAcctId){
		Assert.isTrue(!StringUtils.isEmpty(custAcctId));
		TbSettAccount account = settAccountService.findByCustAcctId(custAcctId);
		model.addAttribute("FuncFlags", dict.findByDictType("TB_SETT_FREEZE.FUNC_FLAG"));
		model.addAttribute("ids", dict.mapByDictType("TB_SETT_ACCOUNT.ID_TYPE"));
		model.addAttribute("account", account);
		return "/account/freeze";
	}
	
	/**
	 * 冻结/解冻操作 
	 * @param funcFlag 操作标记
	 * @param custAcctId 子账号
	 * @param thirdCustId 会员账号
	 * @param tranAmount 金额
	 * @return
	 */
	@RequestMapping("/saveFreeze")
	@ResponseBody
	public Answer saveFreeze(int funcFlag,@RequestParam("id.custAcctId") String custAcctId, @RequestParam("id.thirdCustId") String thirdCustId,double tranAmount) {
		logger.info("saveFreeze   funcFlag:{} custAcctId:{} thirdCustId:{} tranAmount:{}",funcFlag,custAcctId,thirdCustId,tranAmount);
		Answer rsp = settAccountService.saveFreeze(funcFlag, custAcctId, thirdCustId, tranAmount);
		
		return rsp;
	}

	/**
	 * 收费与退费操作界面
	 * @param model
	 * @param custAcctId
	 * @return
	 */
	@RequestMapping("platCharge")
	public String platformChargePage(Model model,String custAcctId){
		
		Assert.isTrue(!StringUtils.isEmpty(custAcctId));
		TbSettAccount account = settAccountService.findByCustAcctId(custAcctId);
		model.addAttribute("FuncFlags", dict.findByDictType("TB_SETT_HANDFEE.FEE_TYPE.1.FUNC_FLAG"));
		model.addAttribute("ids", dict.mapByDictType("TB_SETT_ACCOUNT.ID_TYPE"));
		model.addAttribute("account", account);
		
		return "/account/platCharge";
	}
	
	/**
	 * 保存收费与退费的处理结果
	 * @param funcFlag
	 * @param custAcctId
	 * @param thirdCustId
	 * @param tranAmount
	 * @return
	 */
	@RequestMapping("/saveHee")
	@ResponseBody
	public Answer saveHee(int funcFlag,@RequestParam("id.custAcctId") String custAcctId, @RequestParam("id.thirdCustId") String thirdCustId,double tranAmount){
		logger.info("saveHee   funcFlag:{} custAcctId:{} thirdCustId:{} tranAmount:{}",funcFlag,custAcctId,thirdCustId,tranAmount);
		Answer rsp = settAccountService.saveHee(funcFlag, custAcctId, thirdCustId, tranAmount);
		return rsp;
	}
	
	
	/**
	 * 平台支付与收取
	 * @param model
	 * @param custAcctId
	 * @return
	 */
	@RequestMapping("platSett")
	public String platformSettPage(Model model,String custAcctId){
		
		Assert.isTrue(!StringUtils.isEmpty(custAcctId));
		TbSettAccount account = settAccountService.findByCustAcctId(custAcctId);
		model.addAttribute("FuncFlags", dict.findByDictType("TB_SETT_HANDFEE.FEE_TYPE.2.FUNC_FLAG"));
		model.addAttribute("ids", dict.mapByDictType("TB_SETT_ACCOUNT.ID_TYPE"));
		model.addAttribute("account", account);
		
		return "/account/platSett";
	}
	
	/**
	 * 保存平台收付与支取的结果
	 * @param funcFlag
	 * @param custAcctId
	 * @param thirdCustId
	 * @param tranAmount
	 * @return
	 */
	@RequestMapping("/saveSett")
	@ResponseBody
	public Answer saveSett(int funcFlag,@RequestParam("id.custAcctId") String custAcctId, @RequestParam("id.thirdCustId") String thirdCustId,double tranAmount){
		logger.info("saveSett   funcFlag:{} custAcctId:{} thirdCustId:{} tranAmount:{}",funcFlag,custAcctId,thirdCustId,tranAmount);
		Answer rsp = settAccountService.saveSett(funcFlag, custAcctId, thirdCustId, tranAmount);
		return rsp;
	}
	
	
	
	/**
	 * 入金操作的界面
	 * @param model
	 * @param custAcctId
	 * @return
	 */
	@RequestMapping("/inMoney")
	public String inMoneyPage(Model model,String custAcctId){
		
		Assert.isTrue(!StringUtils.isEmpty(custAcctId));
		TbSettAccount account = settAccountService.findByCustAcctId(custAcctId);
		model.addAttribute("account", account);
		return "/account/inMoney";
	}
	
	/**
	 * 执行入金操作
	 * @param model
	 * @param custAcctId
	 * @return
	 */
	@RequestMapping("/saveInMoney")
	@ResponseBody
	public Answer saveInMoney(@RequestParam("id.custAcctId") String custAcctId, @RequestParam("id.thirdCustId") String thirdCustId,double tranAmount){
		logger.info("saveInMoney  custAcctId:{} thirdCustId:{} tranAmount:{}",custAcctId,thirdCustId,tranAmount);
		Answer rsp = settAccountService.saveInMoney(custAcctId, thirdCustId, tranAmount);
		return rsp;
	}
	
	
	/**
	 * 出金操作的界面
	 * @param model
	 * @param custAcctId
	 * @return
	 */
	@RequestMapping("/outMoney")
	public String outMoneyPage(Model model,String custAcctId){
		
		Assert.isTrue(!StringUtils.isEmpty(custAcctId));
		TbSettAccount account = settAccountService.findByCustAcctId(custAcctId);
		model.addAttribute("account", account);
		return "/account/outMoney";
	}
	
	/**
	 * 执行入金操作
	 * @param model
	 * @param custAcctId
	 * @return
	 */
	@RequestMapping("/saveOutMoney")
	@ResponseBody
	public Answer saveOutMoney(@RequestParam("id.custAcctId") String custAcctId, @RequestParam("id.thirdCustId") String thirdCustId,double tranAmount){
		logger.info("saveInMoney  custAcctId:{} thirdCustId:{} tranAmount:{}",custAcctId,thirdCustId,tranAmount);
		Answer rsp = settAccountService.saveOutMoney(custAcctId, thirdCustId, tranAmount);
		return rsp;
	}
	
	/**
	 * 同步资金账户
	 * @param custAcctId
	 * @return
	 */
	@RequestMapping("/sync")
	@ResponseBody
	public Answer sync(String custAcctId){
		logger.info("sync  custAcctId:{} ",custAcctId);
		Assert.notNull(custAcctId,"需要传入客户虚拟账号");
		return settFundService.syncFundInfo(custAcctId);
		
	}
	
	/**
	 * 强制支付
	 * @param model
	 */
	@RequestMapping("/payForce")
	@ResponseBody
	public Answer payForce(long payId){
		logger.info("强制支付 支付单号:{}",payId);
		return settPayService.payForce(payId,PlatformOperationPaymentPB1331.PAYFORCE);
	}
	
	/**
	 * 代理复核支付
	 * @param payId
	 * @return
	 */
	@RequestMapping("/payAgent")
	@ResponseBody
	public Answer payAgent(long payId){
		logger.info("代理复核支付 支付单号:{}",payId);
		return settPayService.payForce(payId,PlatformOperationPaymentPB1331.PAYAGENT);
	}
	
	/**
	 * 撤销支付
	 * @param payId
	 * @return
	 */
	@RequestMapping("/payCancel")
	@ResponseBody
	public Answer payCancel(long payId){
		logger.info("撤销 支付单号:{}",payId);
		return settPayService.payCancel(payId);
	}
	
	/**
	 * 删除没能通过银行审批的支付订单
	 * @param payId
	 * @return
	 */
	@RequestMapping("/delSettPay")
	@ResponseBody
	public String delSettPay(long payId){
		settPayService.delete(payId);
		return "true";
	}
	
	/**
	 * 解冻交易成功后的金额
	 * @param payId
	 * @return
	 */
	@RequestMapping("/unFreezeSettPay")
	@ResponseBody
	public Answer unFreezeSettPay(long payId){
		return settPayService.unFreezeSettPay(payId);
	}
	
	/**
	 * 本地存储的客户信息流水
	 * @return
	 */
	@RequestMapping("/chgList")
	public String chgList(Model model,String custAcctId){
//		model.addAttribute("chgList", settFundChgService.findByCustAcctId(custAcctId));
		return "/account/chgList";
	}
	
	/**
	 * 本地客户交易流水信息数据
	 * @param page
	 * @param sizePerPage
	 * @param custAcctId
	 * @return
	 */
	@RequestMapping("/fundChgListData")
	@ResponseBody
	public Page<TbSettFundChg> fundChgListData(FunChgQueryVo vo){
		return settFundChgService.findByCustAcctId(vo);
	}
	
	
	@RequestMapping("/")
	public String platInfo(Model model){
		
		model.addAttribute("platInfo", settAccountService.platInfo());
		//获得手续费账户
		
		//获得手续费账户
		model.addAttribute("charges", settFundService.findCharges());
		//获得利息账户
		model.addAttribute("accrual",  settFundService.findAccrual());
		//获得清收子账户
		model.addAttribute("settlement",  settFundService.findSettlement());
		return "platInfo";
		
	}
}
